#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h> // ftok
#include <sys/ipc.h> // shmget ftok
#include <sys/shm.h> // shmget
using namespace std;
#define PATH_NAME "/home/rtx2"
#define SHM_SIZE 4096 //共享内存的大小，最好是页(PAGE: 4096)的整数倍，多1字节也会多创建一页

int main()
{
    key_t key1 = ftok(PATH_NAME, 0x11);
    key_t key2 = ftok(PATH_NAME, 0x22);
    key_t key3 = ftok(PATH_NAME, 0x33);
    key_t key4 = ftok(PATH_NAME, 0x44);
    key_t key5 = ftok(PATH_NAME, 0x55);

    printf("key1: 0x%x\n", key1);
    printf("key2: 0x%x\n", key2);
    printf("key3: 0x%x\n", key3);
    printf("key4: 0x%x\n", key4);
    printf("key5: 0x%x\n", key5);

    int shmid1 = shmget(key1, SHM_SIZE, IPC_CREAT | IPC_EXCL);
    int shmid2 = shmget(key2, SHM_SIZE, IPC_CREAT | IPC_EXCL);
    int shmid3 = shmget(key3, SHM_SIZE, IPC_CREAT | IPC_EXCL);
    int shmid4 = shmget(key4, SHM_SIZE, IPC_CREAT | IPC_EXCL);
    int shmid5 = shmget(key5, SHM_SIZE, IPC_CREAT | IPC_EXCL);

    cout << "shmid1: " << shmid1 << endl;
    cout << "shmid2: " << shmid2 << endl;
    cout << "shmid3: " << shmid3 << endl;
    cout << "shmid4: " << shmid4 << endl;
    cout << "shmid5: " << shmid5 << endl;

    shmctl(shmid1, IPC_RMID, nullptr);
    shmctl(shmid2, IPC_RMID, nullptr);
    shmctl(shmid3, IPC_RMID, nullptr);
    shmctl(shmid4, IPC_RMID, nullptr);
    shmctl(shmid5, IPC_RMID, nullptr);

    return 0;
}